package cn.ggsaas.user.controller;

import cn.ggsaas.user.api.SysUserService;
import cn.ggsaas.user.pojo.SysUser;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.HashMap;

/**
 * <p>
 * 作业系统的用户表 前端控制器
 * </p>
 *
 * @author ggsaas-MpGenerator
 * @since 2020-09-01
 */
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Reference
    private SysUserService userService;

    /**
     * 获取指定用户全部信息
     * @return 用户信息
     */
    @GetMapping("/info/{username}")
    public R info(@PathVariable String username) {
        SysUser user = userService.getOne(Wrappers.<SysUser>query().lambda().eq(SysUser::getName, username));
        if (user == null) {
            return R.failed(String.format("用户信息为空 %s", username));
        }
        return R.ok(userService.getUserInfo(user));
    }

    @GetMapping
    public Collection<SysUser> query(String name) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", name);
        Collection<SysUser> sysUsers = userService.listByMap(map);
        return sysUsers;
    }

    @PostMapping
    public SysUser create(@RequestBody SysUser user) {
        return user;
    }

    @PutMapping
    public SysUser update(@RequestBody SysUser user) {
        return user;
    }

    @DeleteMapping
    public void delete(@RequestBody Long id) {
    }

    @GetMapping("/{id}")
    public SysUser get(@PathVariable Long id) {
        return new SysUser();
    }
}

